<?php
defined('BASEPATH') OR exit('No direct script access allowed');
	class User extends CI_Controller{

		public function login(){
			$this->load->view('ads/login');
		}
		public function register(){
			$this->load->view('ads/register');
		}

		//登陆页面
		public function signin(){
			$this->load->model('Usermodel');
			$username = $this->input->post('username');
			$password = $this->input->post('password');

			if( $user = $this->Usermodel->get_user($username,$password)){
				#ok
				$this->load->library('session');//加载session类
		        $this->session->login_user = $user;
				// redirect("Main/index");
				$this->load->view("home/index");
			} else{
				#error
				echo "<script>alert('账号或密码错误')</script>";
				$this->load->view("ads/login");
				// redirect("user/login");
			}
		}

		//注册页面
		public function inregister(){
			$this->load->model('Usermodel');
			//表单验证
			$this->load->helper(array('form', 'url'));
        	$this->load->library('form_validation');
        	$this->form_validation->set_rules('regname', '用户名', 'required|max_length[16]');
			$this->form_validation->set_rules('password', '密码', 'required|min_length[6]|max_length[16]');
			$this->form_validation->set_rules('regpassword', '确认密码', 'required|matches[password]');

			if ($this->form_validation->run() == FALSE){
		            // $this->load->view('myform');
		            echo "<script>alert('格式错误,请重新填写')</script>";
					$this->load->view("ads/register");
		    } else {
		            $data["name"] = $this->input->post("regname");
		            $data["password"] = $this->input->post("password");
		            $data["quanxian"] = time();
		            if(!$row_user = $this->Usermodel->alike_user($data["name"])){
		            	echo "<script>alert('用户名已存在')</script>";
		            	$this->load->view("ads/register");
		            }else{
			            if ($this->user_model->add_user($data)) {
			            	# 注册成功
			            	echo "ok";
			            	redirect("User/login");
			            }else{
			            	echo "<script>alert('注册失败，请重新注册')</script>";
							$this->load->view("ads/register");
			            	// redirect("user/register");

			            }
			        }
		    }
		}
		//退出按钮
		public function quit(){
			$this->load->library('session');
			$this->session->unset_userdata('name');
			$this->login();

		}
		
	}
?>